package com.bos.controller;

import com.bos.controller.base.BaseController;
import com.bos.controller.base.imp.BaseControllerImp;
import com.bos.domain.Function;
import com.bos.domain.User;
import com.bos.service.IFuntionService;
import com.bos.utils.BOSUtil;
import com.bos.utils.PageBean;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author Administrator
 */
@Controller
public class FunctionController extends BaseControllerImp<Function> implements BaseController<Function> {


    @Autowired
    private IFuntionService funtionService;


    @Autowired
    public FunctionController(PageBean pageBean){
        super(pageBean);
    }


    /**查询权限数据返回easy-ui权限树的json格式数据
     * */
    @RequestMapping(value = "/ListParentFunctions",method = RequestMethod.POST)
    @ResponseBody
    public String ListParentFunctions(){
        List<Function> functions = funtionService.findAll();
        String json = java2json(functions, new String[]{"roles", "description" +
                "code", "zindex","parentFunction"});
        return json;
    }



    @RequestMapping(value = "/addFuntion",method = RequestMethod.POST)
    public String addFuntion(Function function){
        Function parentFunction = function.getParentFunction();
        if (parentFunction != null && parentFunction.getId().equals("")) {
            parentFunction.setParentFunction(null);
        }
        funtionService.save(function);
        return "redirect:/page/admin/funtion";
    }


    @RequestMapping(value = "/funtionPageQuery",method = RequestMethod.GET)
    @ResponseBody
    public String funtionPageQuery(int page ,int rows){
        //将当前页号和页面大小至于session域中，以便刷新时保持不变
        BOSUtil.getSession().setAttribute("funtionPage",page);
        BOSUtil.getSession().setAttribute("funtionRows",rows);
        setPageAndRows(page,rows);
        funtionService.pageQuery(super.pageBean);
        return java2json(new String[]{"currentPage","pageSize","detachedCriteria","functions",
                "users","parentFunction"});
    }


    /**
     * 根据当前用户查询菜单
     * */
    @RequestMapping(value = "/findUserMenu",method = RequestMethod.GET)
    @ResponseBody
    public String findUserMenu(){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        List<Function> functions;
        String admin = "admin";
        if (admin.equals(user.getUsername())){
            //超级用户
            functions = funtionService.findAllMenu();
        }else {
            //普通用户
            functions = funtionService.findMenuByUserId(user.getId());
        }
        String json = java2json(functions,
                new String[]{"roles", "description" +
                        "code", "zindex","parentFunction"});
        return json;
    }



}
